angular.module('mamApp').controller('adminUserController', ['$scope', '$state', '$uibModal', 'SystemManager', 'ImsDialog', 'RoleManager',
    function ($scope, $state, $uibModal, SystemManager, ImsDialog, RoleManager) {


        $scope.search = {
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10,
            fullName: $state.params.fullName || "",
            departmentId: $state.params.departmentId ? parseInt($state.params.departmentId) : "",
            //secDepartmentId: parseInt($state.params.secDepartmentId) || "",
            secDepartmentId: $state.params.secDepartmentId ? parseInt($state.params.secDepartmentId) : "",
            roleId: $state.params.roleId ? parseInt($state.params.roleId) : ""
        };

        //一级部门初始
        SystemManager.departmentsList().then(function (result) {
            $scope.departments = [{departmentId: "", departmentName: "一级部门"}].concat(result.data.departments);
        });

        //二级部门初始
        $scope.secDepartments = [{departmentId: "", departmentName: "二级部门"}];

        //一级部门变化
        $scope.departmentChange = function () {
            $scope.search.pageNo=1;
            $scope.secDepartments = [{departmentId: "", departmentName: "二级部门"}];
            $scope.search.secDepartmentId = "";
            $state.go($state.current.name, $scope.search)
        };

        //二级部门变化
        if ($scope.search.departmentId) {
            SystemManager.departmentsList({firstId: $scope.search.departmentId}).then(function (result) {
                $scope.secDepartments = $scope.secDepartments.concat(result.data.departments);
            })
        }

        $scope.secDepartmentChange = function () {
            $state.go($state.current.name, $scope.search)
        };

/*        SystemManager.departmentsList().then(function (result) {
            $scope.departments = [];
            $scope.departments = [{departmentId: "", departmentName: "一级部门"}].concat(result.data.departments);
            $scope.secDepartments = [{departmentId: "", departmentName: "二级部门"}]
        });

        $scope.departmentChange = function (departmentId) {
            $scope.secDepartments = [];
            SystemManager.departmentsList({firstId: departmentId}).then(function (result) {
                $scope.secDepartments = [{departmentId: "", departmentName: "二级部门"}].concat(result.data.departments);
                console.log($scope.secDepartments)
            });

            //$state.go($state.current.name, $scope.search)
        };

        if ($scope.search.departmentId) {
            $scope.departmentChange($scope.search.departmentId)
        }


        $scope.secDepartmentChange = function (departmentId) {

            $scope.search.secDepartmentId = departmentId;
            $scope.search.departmentId = departmentId ? parseInt(departmentId.toString().substring(0, 3)) : "";
            $state.go($state.current.name, $scope.search)

        }*/

        //角色selset
        RoleManager.roleListSimple().then(function (result) {
            $scope.roleList = [{roleId: "", roleName: "角色"}].concat(result.data.list);
        });

        $scope.categoryChange = function () {
            $scope.search.pageNo=1;
            $state.go($state.current.name, $scope.search)

        };

        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        };

        $scope.delete = function (item) {
            ImsDialog.open({title: "用户删除", message: "确定删除" + item.email + "吗？"}).then(function (a) {
                if (a == "ok") {
                    SystemManager.adminUserDelete(item.adminId).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })

        }

        $scope.noPermission = false;
        SystemManager.adminUserList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            if (result.data.c == "n") {
                $scope.noPermission = true;
            } else {
                $scope.noPermission = false;
                $scope.adminList = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            }

        })


        $scope.keyup = function (e, keyword) {

            if (e.key == "Enter") {
                $scope.search.keyword = keyword;
                $scope.search.reviewStatus = $scope.search.reviewStatus == "null" ? null : $scope.search.reviewStatus;
                $state.go($state.current.name, $scope.search)
            }
        }


    }])

